﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Community
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Community;
    using LdCms.IDAL.Community;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;

    public partial class VThemeReplyService:BaseService<V_Community_ThemeReply>,IVThemeReplyService
    {
        private readonly IVThemeReplyDAL VThemeReplyDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public VThemeReplyService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IVThemeReplyDAL VThemeReplyDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.VThemeReplyDAL = VThemeReplyDAL;
            this.Dal = VThemeReplyDAL;
        }
        public override void SetDal()
        {
            Dal = VThemeReplyDAL;
        }


        public V_Community_ThemeReply GetThemeReply(int systemId, string companyId, string themeId,string replyId)
        {
            try
            {
                var expression = ExtLinq.True<V_Community_ThemeReply>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId&&m.ReplyID== replyId);
                return Find(expression);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<V_Community_ThemeReply> GetThemeReplyTop(int systemId, string companyId, int count)
        {
            try
            {
                int total = Utility.ToTopTotal(count);
                var expression = ExtLinq.True<V_Community_ThemeReply>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                return FindListTop(expression, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<V_Community_ThemeReply> GetThemeReplyPaging(int systemId, string companyId, int pageId, int pageSize)
        {
            try
            {
                var expression = ExtLinq.True<V_Community_ThemeReply>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                int pageIndex = Utility.ToPageIndex(pageId);
                int pageCount = Utility.ToPageCount(pageSize);
                return FindListPaging(expression, m => m.CreateDate, false, pageIndex, pageCount).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<V_Community_ThemeReply> SearchThemeReply(int systemId, string companyId, string startTime, string endTime, string columnId, string state, string keyword, int count)
        {
            try
            {
                DateTime dateStartTime = Utility.ToStartTime(startTime);
                DateTime dateEndTime = Utility.ToEndTime(endTime);
                bool blnState = state.ToBool();
                int total = Utility.ToTopTotal(count);
                //条件
                var expression = ExtLinq.True<V_Community_ThemeReply>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId
                && m.CreateDate.Value.Date >= dateStartTime.Date && m.CreateDate.Value.Date <= dateEndTime.Date
                && (string.IsNullOrEmpty(columnId) ? true : m.ColumnID == columnId)
                && (string.IsNullOrEmpty(state) ? true : m.State.Value == blnState)
                && (m.MemberID.Contains(keyword) || m.Title.Contains(keyword)));
                //执行
                return FindListTop(expression, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public int CountThemeReply(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
